﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;

namespace Hired
{
    public partial class emplogin : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            if (txtUser.Text != "" && txtPassword.Text != "")
            {
                string accountType = DBAccount.RetrieveAccountTypeByUsername(txtUser.Text);
                switch (accountType)
                {
                    case "EmployerPassport":
                        EmployerPassport passportRetrieved = DBEmployerPassport.RetrieveEmployerPassportByUsername(txtUser.Text);
                        if (passportRetrieved != null)
                        {
                            if (Encryption.verify(txtPassword.Text, passportRetrieved.Password))
                            {
                                if ((bool)passportRetrieved.Activated)
                                {
                                    Session["username"] = passportRetrieved.Username;
                                    FormsAuthentication.RedirectFromLoginPage(passportRetrieved.EPassportID, true);
                                    Response.Redirect("home.aspx");
                                }
                                else
                                {
                                    Session["usernameToActivate"] = passportRetrieved.Username;
                                    Response.Redirect("notactivated.aspx", false);
                                }
                            }
                        }
                        break;
                    case "CompanyAccount":
                        CompanyAccount companyAccount = DBCompanyAccount.RetrieveCompanyAccountByUsername(txtUser.Text, false);
                        if (companyAccount != null)
                        {
                            if (Encryption.verify(txtPassword.Text, companyAccount.Password))
                            {
                                if ((bool)companyAccount.Activated)
                                {
                                    Session["username"] = companyAccount.Username;
                                    FormsAuthentication.RedirectFromLoginPage(companyAccount.CAccountID, true);
                                    Response.Redirect("home.aspx");
                                }
                                else
                                {
                                    Session["usernameToActivate"] = companyAccount.Username;
                                    Response.Redirect("notactivated.aspx", false);
                                }
                            }
                        }
                        break;
                }
                lblErrorMsg.Text = "Wrong Username or Password";
            }
            else
            {
                lblErrorMsg.Text = "Please fill in all the fields";
            }
        }
    }
}